//
//  MainViewController.swift
//  Weibo
//
//  Created by 5201-mac on 2017/4/15.
//  Copyright © 2017年 wlb-mac. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //添加首页
        addChildViewController(sbName: "Home", title: "首页", imageName: "tabbar_home")
        //添加消息
        addChildViewController(sbName: "Message", title: "消息", imageName: "tabbar_message_center")
        //添加发现
        addChildViewController(sbName: "Discover", title: "发现", imageName: "tabbar_discover")
        //添加我的
        addChildViewController(sbName: "Profile", title: "我的", imageName: "tabbar_profile")
    }

    //MARK: 添加子视图控制器
    func addChildViewController(sbName: String,title: String,imageName: String) {
        //添加子视图控制器
        let sb = UIStoryboard(name: sbName, bundle: nil)
        let nav = sb.instantiateInitialViewController() as! UINavigationController
        
        //修改文字
        nav.topViewController?.navigationItem.title = title
        nav.topViewController?.tabBarItem.title = title
        nav.topViewController?.tabBarItem.image = UIImage(named: imageName)
        //设置选中时的图片,barbuttonItem设置的图片都会被系统自动渲染成对应的titnColor对应的颜色
        nav.topViewController?.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        //tabBar.tintColor = UIColor.orange
        //tabBar.barTintColor = UIColor.purple
        //修改文字颜色
        nav.topViewController?.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
        //设置文字的位移
        nav.topViewController?.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3)
        self.addChildViewController(nav)
    }
    

}
